package com.cci.kangdao.task;

import com.cci.kangdao.dao.UserTDao;
import com.cci.kangdao.dao.WeChatGetMaterialDao;
import com.cci.kangdao.service.PropertiesUtils;
import com.cci.kangdao.utilTool.CollectionUtils;
import com.cci.kangdao.utilTool.LocalTimeUtil;
import com.cci.kangdao.utilTool.MailUtil;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@Component
public class FeedBackDealTask {
    private Logger log = Logger.getLogger(this.getClass().getName());

    @Autowired
    private UserTDao userTDao;

    @Resource
    private PropertiesUtils propertiesUtils;


    @Resource
    private WeChatGetMaterialDao weChatGetMaterialDao;

    //每天八点发送邮件
    //因为服务器时间比现实时间早八小时，所以这里设置每天0点定时推送(相当于现实时间的八点推送)
    public void dealFeedBack(){
        log.info("执行定时任务:每天八点发送e路康明斯工单处理提醒邮件：当前时间为:"+ LocalTimeUtil.getRealCurrentTime());
        try {
            //获取意见反馈表中，工单状态为处理中的处理人和工单信息
            List<Map<String, Object>> notDealFeedBackUserList = userTDao.getNotDealFeedBackUserList();
            if (CollectionUtils.isNotEmpty(notDealFeedBackUserList)) {
                for (Map<String, Object> notDealFeedBackUser : notDealFeedBackUserList) {
                    String email = MapUtils.getString(notDealFeedBackUser, "Email", "");  //邮箱
                    String billNo = MapUtils.getString(notDealFeedBackUser, "BillNo", "");  //工单编号
                    String feedbackQuestion = MapUtils.getString(notDealFeedBackUser, "Content", "");//反馈的问题
                    String feedBackReport = propertiesUtils.getPropertiesValue("feedBackReport");  //意见反馈的url
                    feedBackReport += "&billNo=" + billNo;
                    if (StringUtils.isNotEmpty(email)) {
                        String subject = "e路康明斯工单处理提醒：" + billNo;  //邮件主题
                        String content = "<body>尊敬的用户你好:<br><p style='text-indent: 2em;'>您有未处理的工单，请及时处理！</p><p style='text-indent: 2em;'>工单编号：" + billNo + "</p><p style='text-indent: 2em;'>反馈问题：" + feedbackQuestion + "</p><p style='text-indent: 2em;'><a href='" + feedBackReport + "'>前往移动应用管理平台处理工单</a></p></body>";  //邮件内容
                        try {
                            MailUtil.sendMail(email, subject, content);  //推送邮件
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        } catch (Exception e) {
            log.error("定时任务dealFeedBack发生异常，异常信息" + e.getMessage(), e);
        }

        log.info("执行发送邮件定时任务完毕!");
    }



}
